package com.qf.Web.Servlet;

import com.alibaba.fastjson.JSON;
import com.qf.Domain.WeiXin;
import com.qf.Service.Impl.OrderServiceImpl;
import com.qf.Service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/*
 *
 */
@WebServlet(name = "PayServlet",value = "/paySuccess")
public class PayServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String result = request.getParameter("result");
        WeiXin weiXin = JSON.parseObject(result, WeiXin.class);
        if (weiXin.getType()==0){
            if (weiXin.getResult().getResultCode().equals("SUCCESS")){//重定向
                //成功修改订单状态
                OrderService orderService=new OrderServiceImpl();
                String oid=weiXin.getResult().getOutTradeNo();
                orderService.updateStatus(weiXin.getResult().getOutTradeNo(),"2");
                request.setAttribute("msg", oid+"支付成功,我们会尽快安排发货");
            }
        }else {
            //点对点
        }
        request.getRequestDispatcher("/message.jsp").forward(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
